﻿<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title>测试</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <link rel="stylesheet" type="text/css" href="../../../layui/dist/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="../../../styles/reportpage.css" />
    <script type="text/javascript" src="../../../Scripts/json2.js"></script>
    <script type="text/javascript" src="../../../Scripts/jquery-1.11.2.min.js"></script>
    <style type="text/css">
        .layui-btn + .layui-btn {
            margin-left: 0px;
        }

        td[data-field="mfgordername"] div, td[data-field="productname"] div, td[data-field="description"] div {
            text-align: left !important;
        }
    </style>
</head>
<body>
    <!--[if lt IE 9]>
        <script src="../../../Scripts/pollyfills/html5.js"></script>
        <script src="../../../Scripts/pollyfills/response.js"></script>
        <script type="text/javascript">
            if ( !Array.prototype.forEach ) {
                Array.prototype.forEach = function forEach( callback, thisArg ) {
                    var T, k;
                    if ( this == null ) {
                        throw new TypeError( "this is null or not defined" );
                    }
                    var O = Object(this);
                    var len = O.length >>> 0;
                    if ( typeof callback !== "function" ) {
                        throw new TypeError( callback + " is not a function" );
                    }
                    if ( arguments.length > 1 ) {
                        T = thisArg;
                    }
                    k = 0;
                    while( k < len ) {
                        var kValue;
                        if ( k in O ) {
                            kValue = O[ k ];
                            callback.call( T, kValue, k, O );
                        }
                        k++;
                    }
                };
            }
        </script>
    <![endif]-->
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 60px;">
                    分厂：
                </label>
                <div class="layui-input-inline" style="width: 160px;">
                    <select name="ddlFactory" lay-verify="required" id="ddlFactory" lay-filter="ddlFactory"
                            lay-search="">
                        <option value=""></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 60px;">
                    订单号:
                </label>
                <div class="layui-input-inline">
                    <input id="txtMfgordername" type="text" name="txtMfgordername" class="layui-input" />
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 80px;">
                    零件图号:
                </label>
                <div class="layui-input-inline">
                    <input id="txtProductname" type="text" name="txtProductname" class="layui-input" />
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label" style="width:80px;">
                    完工日期：
                </label>
                <div class="layui-input-inline">
                    <input id="txtStartDate" type="text" name="txtStartDate" class="layui-input" />
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:60px;">
                    至：
                </label>
                <div class="layui-input-inline">
                    <input id="txtEndDate" type="text" name="txtEndDate" class="layui-input" />
                </div>
            </div>

            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-sm" id="btnSearch">
                    <i class="layui-icon" style="font-weight: bold;">&#xe615;</i>查询
                </button>
                <button type="button" class="layui-btn layui-btn-sm" id="btnReset">
                    <i class="layui-icon" style="font-weight: bold;">&#xe9aa;</i>重置
                </button>
            </div>
        </div>
        <div class="layui-fluid">
            <div class="layui-row">
                <div class="layui-col-md12">
                    <table class="layui-table" id="grid" lay-data="{text:{ none: '暂无相关数据'}}" lay-filter="grid">
                        <thead>
                            <tr>
                                <th lay-data="{ type: 'numbers', title: '序号', width: 60 }">
                                </th>
                                <!--<th lay-data="{ type: 'checkbox', title: '选择', width: 60 }"></th>-->
                                <th lay-data="{ title: '分厂' }">
                                </th>
                                <th lay-data="{ title: '订单号' }">
                                </th>
                                <th lay-data="{ title: '数量', width: 70 }">
                                </th>
                                <th lay-data="{ title: '零件图号' }">
                                </th>
                                <th lay-data="{ title: '零件名称' }">
                                </th>
                                <th lay-data="{ title: '初始' }">
                                </th>
                                <th lay-data="{ title: '追赶' }">
                                </th>
                                <th lay-data="{ title: '调整' }">
                                </th>
                                <th lay-data="{ title: '开工日期' }">
                                </th>
                                <th lay-data="{ title: '完工日期' }">
                                </th>
                                <th lay-data="{ title: '操作', width: 90, toolbar: '#barDemo'}">
                                </th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </form>
    <script type="text/html" id="barDemo">
        <button type="button" class="layui-btn layui-btn-xs" lay-event='save'>
            <i class="layui-icon layui-icon-ok"></i>保存
        </button>
    </script>
    <script type="text/javascript" src="../../../layui/dist/layui.js"></script>
    <script type="text/javascript">
        $(function () {
            layui.use(['form', 'layer', 'table', 'laydate'], function () {
                var $ = layui.$;
                var form = layui.form;
                var laydate = layui.laydate;
                var table = layui.table;
                var layer = layui.layer;
                var grid = null;
                var userInfo = null;
                var type = layui.device().ie === false ? layui.url().search.type : layui.url().search.type.split("?")[0];
                var column;
                var index = null;

                var now = new Date();
                var currentYear = now.getFullYear();
                var currentMonth = now.getMonth() + 1;
                var currentMonthLastDay = new Date(currentYear, currentMonth, 0).getDate();
                if (currentMonth < 10) {
                    currentMonth = "0" + currentMonth;
                };

                var startDate = currentYear + "-" + currentMonth + "-" + "01";
                var endDate = currentYear + "-" + currentMonth + "-" + currentMonthLastDay;

                //绑定时间
                function BindLaydate(id, value) {
                    laydate.render({
                        elem: '#' + id,
                        type: 'date',
                        trigger: 'click',
                        lang: 'cn',
                        calendar: false,
                        theme: 'molv',
                        range: false,
                        value: value
                    });
                };
                BindLaydate("txtStartDate", startDate);
                BindLaydate("txtEndDate", endDate);


                //点击分页按钮显示加载动画
                function BindPage() {
                    $(".layui-table-page a[data-page],.layui-laypage-btn").click(function () {
                        if (!$(this).hasClass("layui-disabled")) {
                            index = layer.load(0, { shade: [0.5, '#000'], time: 1000 * 1000 });
                        }
                    });
                }

                //查询用户信息
                function GetUserInfo() {
                    $.ajax({
                        type: "POST",
                        url: "MfgorderMaint.ashx?type=GetUserInfo",
                        dataType: "text",
                        cashe: false,
                        success: function (result) {
                            var data = JSON.parse(result);
                            if (data.FactoryID) {
                                //保存用户信息
                                userInfo = data;
                                var select = 'dd[lay-value=' + data.FactoryID + ']';
                                $('#ddlFactory').siblings("div.layui-form-select").find('dl').find(select).click();
                                $('#ddlFactory').eq(0).attr({ "disabled": "disabled" });
                                form.render("select");
                            }
                        }
                    });
                }

                function GetFactory() {
                    $.ajax({
                        type: "POST",
                        url: "MfgorderMaint.ashx?type=GetFactory",
                        dataType: "text",
                        cashe: false,
                        success: function (result) {
                            var json = JSON.parse(result);
                            $("#ddlFactory").empty();
                            $('#ddlFactory').append("<option value='全部'>全部</option>");
                            for (var i = 0; i < json.length; i++) {
                                $('#ddlFactory').append("<option value='" + json[i]["factoryid"] + "'>" + json[i]["factoryname"] + "</option>");
                            }
                            form.render("select");
                            GetUserInfo();
                        }
                    });
                }
                GetFactory();

                //获取查询条件
                function GetQueryObject() {
                    var mfgordername = $("#txtMfgordername").val() === "" ? "空" : $("#txtMfgordername").val();
                    var productname = $("#txtProductname").val() === "" ? "空" : $("#txtProductname").val();
                    var factoryid = $("#ddlFactory").val();
                    var startdate = $("#txtStartDate").val() === "" ? "空" : $("#txtStartDate").val();
                    var enddate = $("#txtEndDate").val() === "" ? "空" : $("#txtEndDate").val()

                    return {
                        mfgordername: mfgordername,
                        productname: productname,
                        factoryid: factoryid,
                        startdate: startdate,
                        enddate: enddate
                    };
                };

                function GetData() {
                    index = layer.load(0, { shade: [0.5, '#000'], time: 1000 * 1000 });
                    if (type == "tiaozheng") {
                        grid = table.render({
                            elem: '#grid'
                            , url: "MfgorderMaint.ashx?type=GetData"
                            , method: 'POST'
                            , where: GetQueryObject()
                            , text: {
                                none: '暂无相关数据'
                            }
                            , request: {
                                pageName: 'page',
                                limitName: 'rows'
                            }
                            , page: {
                                theme: '#009688'
                            }
                            , limit: 10
                            , limits: [10, 20, 30, 50, 100]
                            , cols: [[
                                { type: 'numbers', title: '序号', width: 60 },
                                { field: 'factoryname', title: '分厂' },
                                { field: 'mfgorderid', title: '订单id', hide: true },
                                { field: 'mfgordername', title: '订单号' },
                                { field: 'qty', title: '数量', width: 70 },
                                { field: 'productname', title: '零件图号' },
                                { field: 'description', title: '零件名称' },
                                {
                                    field: 'origindate', title: '初始', width: 140,
                                    templet: function (d) {
                                        if (d.origindate === null) {
                                            return "";
                                        } else {
                                            return d.origindate.split('T')[0];
                                        }
                                    }
                                },
                                {
                                    field: 'zhuigandate', title: '追赶',
                                    templet: function (d) {
                                        if (d.zhuigandate === null) {
                                            return "";
                                        } else {
                                            return d.zhuigandate.split('T')[0];
                                        }
                                    }
                                },
                                {
                                    field: 'tiaozhengdate', title: '调整', edit: 'text', event: 'editStartDate', data_field: '',
                                    templet: function (d) {
                                        if (d.tiaozhengdate === null) {
                                            return "";
                                        } else {
                                            return d.tiaozhengdate.split('T')[0];
                                        }
                                    }
                                },
                                {
                                    field: 'startdate', title: '开工日期',
                                    templet: function (d) {
                                        if (d.startdate === null) {
                                            return "";
                                        } else {
                                            return d.startdate.split('T')[0];
                                        }
                                    }
                                },
                                {
                                    field: 'plannedcompletiondate', title: '完工日期',
                                    templet: function (d) {
                                        if (d.plannedcompletiondate === null) {
                                            return "";
                                        } else {
                                            return d.plannedcompletiondate.split('T')[0];
                                        }
                                    }
                                },
                                { title: '操作', width: 90, toolbar: '#barDemo' }
                            ]]
                            , done: function (res, curr, count) {
                                BindPage();
                                layer.close(index);
                                $(".layui-laypage-limits select").change(function () {
                                    index = layer.load(0, { shade: [0.5, '#000'], time: 1000 * 1000 });
                                });
                            }
                        });
                    }
                    else {
                        grid = table.render({
                            elem: '#grid'
                            , url: "MfgorderMaint.ashx?type=GetData"
                            , method: 'POST'
                            , where: GetQueryObject()
                            , text: {
                                none: '暂无相关数据'
                            }
                            , request: {
                                pageName: 'page',
                                limitName: 'rows'
                            }
                            , page: {
                                theme: '#009688'
                            }
                            , limit: 10
                            , limits: [10, 20, 30, 50, 100]
                            , cols: [[
                                { type: 'numbers', title: '序号', width: 60 }
                                , { field: 'factoryname', title: '分厂' }
                                , { field: 'mfgorderid', title: '订单id', hide: true }
                                , { field: 'mfgordername', title: '订单号' }
                                , { field: 'qty', title: '数量', width: 70 }
                                , { field: 'productname', title: '零件图号' }
                                , { field: 'description', title: '零件名称' }
                                , {
                                    field: 'origindate', title: '初始', width: 140,
                                    templet: function (d) {
                                        if (d.origindate === null) {
                                            return "";
                                        } else {
                                            return d.origindate.split('T')[0];
                                        }
                                    }
                                }
                                , {
                                    field: 'zhuigandate', title: '追赶', edit: 'text', event: 'catchDate', data_field: '',
                                    templet: function (d) {
                                        if (d.zhuigandate === null) {
                                            return "";
                                        } else {
                                            return d.zhuigandate.split('T')[0];
                                        }
                                    }
                                }
                                , {
                                    field: 'tiaozhengdate', title: '调整',
                                    templet: function (d) {
                                        if (d.tiaozhengdate === null) {
                                            return "";
                                        } else {
                                            return d.tiaozhengdate.split('T')[0];
                                        }
                                    }
                                }
                                , {
                                    field: 'startdate', title: '开工日期',
                                    templet: function (d) {
                                        if (d.startdate === null) {
                                            return "";
                                        } else {
                                            return d.startdate.split('T')[0];
                                        }
                                    }
                                }
                                , {
                                    field: 'plannedcompletiondate', title: '完工日期',
                                    templet: function (d) {
                                        if (d.plannedcompletiondate === null) {
                                            return "";
                                        } else {
                                            return d.plannedcompletiondate.split('T')[0];
                                        }
                                    }
                                }
                                , { title: '操作', width: 90, toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
                            ]]
                            , done: function (res, curr, count) {
                                BindPage();
                                layer.close(index);
                                $(".layui-laypage-limits select").change(function () {
                                    index = layer.load(0, { shade: [0.5, '#000'], time: 1000 * 1000 });
                                });
                            }
                        });
                    }
                }

                table.on('tool(grid)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'editStartDate') {
                        var field = $(this).data('field');
                        laydate.render({
                            elem: this.firstChild
                            , show: true //直接显示
                            , closeStop: this
                            , done: function (value, date) {
                                data[field] = value;
                                obj.update(data);
                            }
                        });
                    }
                    if (obj.event === 'catchDate') {
                        var field = $(this).data('field');
                        laydate.render({
                            elem: this.firstChild
                            , show: true
                            , closeStop: this
                            , done: function (value, date) {
                                data[field] = value;
                                obj.update(data);
                            }
                        });
                    }
                    if (obj.event === 'save') {
                        var index = layer.load(0, { shade: [0.5, '#000'], time: 1000 * 1000 })
                        $.ajax({
                            type: "POST",
                            url: "MfgorderMaint.ashx?type=Edit",
                            dataType: "text",
                            cashe: false,
                            data: {
                                tiaozhengdate: obj.data.tiaozhengdate === null ? '' : obj.data.tiaozhengdate,
                                zhuigandate: obj.data.zhuigandate === null ? '' : obj.data.zhuigandate,
                                mfgorderid: obj.data.mfgorderid
                            },
                            success: function (result) {
                                var res = JSON.parse(result);
                                layer.close(index);
                                if (res.code === 1) {
                                    layer.alert("订单: " + obj.data.mfgordername + " 信息修改成功！", { icon: 1, title: '提示', time: 1000 });
                                    //更新开工日期、完工日期
                                    obj.data.startdate = res.data[0].startdate;
                                    obj.data.plannedcompletiondate = res.data[0].plannedcompletiondate;
                                    obj.update(data);
                                } else {
                                    layer.alert("订单: " + obj.data.mfgordername + " 信息修改失败！", { icon: 2, title: '提示' });
                                }
                            }
                        });

                    }
                });


                //查询按钮
                $("#btnSearch").click(function () {
                    GetData();
                });

                //重置按钮
                $("#btnReset").click(function () {
                    table.reload('grid', { url: '', data: [] });
                    $("#txtMfgordername").val("");
                    $("#txtProductname").val("");
                });

                //按下回车时执行数据查询
                $(document).keyup(function (event) {
                    if (event.keyCode === 13) {
                        GetData();
                    }
                })

            });
        });
    </script>
</body>
</html>
